package leetcode.median;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * ClassName: $46_Permute
 * Description:
 * date: 2022-02-17 21:13
 *
 * @author liyifan
 */
public class $46_Permute {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean vis[];
    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0) {return res;}
        vis = new boolean[nums.length];
        backtrace(nums);
        return res;
    }

    public void backtrace(int[] nums){
        if (path.size() == nums.length) res.add(new ArrayList<>(path));

        for (int i = 0;i < nums.length;i++) {
            if (vis[i]) continue;

            path.add(nums[i]);
            vis[i] = true;
            backtrace(nums);
            path.remove(path.size() - 1);
            vis[i] = false;
        }
    }
}
